﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.UI.LogicForUserControls;

namespace TCPClient.UI.UserControls
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class DayInAWeekUserControl : UserControl
    {
        private DateTime m_Date;
        private DayOfWeek m_DayOfTheWeek;
        private List<OneShiftUserControl> m_ShiftsInDay =
                new List<OneShiftUserControl>(DefaultConfigurationHandler.Instance.NumberOfShiftsPerDay); 


        public DayInAWeekUserControl()
        {
            InitializeComponent();
        }

        public DateTime Date
        {
            get { return m_Date; }
            set { m_Date = value; }
        }

        public DayOfWeek DayOfWeek
        {
            get { return m_DayOfTheWeek; }
            set { m_DayOfTheWeek = value; }
        }

        public List<OneShiftUserControl>  ShiftsInDay
        {
            get { return m_ShiftsInDay; }
        }

        public void AddControlsOfShifts()
        {
            for (int i = 0; i < DefaultConfigurationHandler.Instance.NumberOfShiftsPerDay; i++)
            {
                OneShiftUserControl shiftUserControl = new OneShiftUserControl();
                shiftUserControl.BorderStyle = BorderStyle.Fixed3D;
                shiftUserControl.Name = string.Format("shift{0}On{1}", i + 1, DayOfWeek.ToString());
                int singleShiftHeight = Height / DefaultConfigurationHandler.Instance.NumberOfShiftsPerDay;
                shiftUserControl.Size = new Size(Width, singleShiftHeight);
                shiftUserControl.Location = new Point(0, i * singleShiftHeight);

                Controls.Add(shiftUserControl);
                m_ShiftsInDay.Add(shiftUserControl);
            }
        }

        public void SetTimesOfShiftCells()
        {
            int i = 0;
            foreach (var shiftUserControl in m_ShiftsInDay)
            {
                shiftUserControl.SetData(m_Date.AddHours(DefaultConfigurationHandler.Instance.LenghtOfShift *
                    i), m_Date.AddHours(DefaultConfigurationHandler.Instance.LenghtOfShift *
                    (i + 1)).AddSeconds(-1));
                i++;
            }
        }

        public void DrawShiftsThatWorkersRegisterdTo(DateTime startTime, DateTime endTime)
        {
            foreach (var shiftCell in m_ShiftsInDay)
            {
                if(shiftCell.StartTime.Date.CompareTo(startTime.Date) == 0
                    && shiftCell.StartTime.Hour.CompareTo(startTime.Hour) == 0 
                    && shiftCell.StartTime.Minute.CompareTo(startTime.Minute) == 0
                    && shiftCell.EndTime.Date.CompareTo(endTime.Date) == 0
                    && shiftCell.EndTime.Hour.CompareTo(endTime.Hour) == 0
                    && shiftCell.EndTime.Minute.CompareTo(endTime.Minute) == 0)
                {
                    shiftCell.SetPicVisible(eRedOrGreenSelected.Blue);
                }
            }
        }

        public void DrawShiftsThatWorkersRequested(DateTime startTime, DateTime endTime, eType type)
        {
            foreach (var shiftCell in m_ShiftsInDay)
            {
                if (shiftCell.StartTime.Date.CompareTo(startTime.Date) == 0
                    && shiftCell.StartTime.Hour.CompareTo(startTime.Hour) == 0
                    && shiftCell.StartTime.Minute.CompareTo(startTime.Minute) == 0
                    && shiftCell.EndTime.Date.CompareTo(endTime.Date) == 0
                    && shiftCell.EndTime.Hour.CompareTo(endTime.Hour) == 0
                    && shiftCell.EndTime.Minute.CompareTo(endTime.Minute) == 0)
                {
                    shiftCell.SetPicVisible(type == eType.WANTED ? eRedOrGreenSelected.Green : eRedOrGreenSelected.Red);
                }
            }
        }

        public void SetTheGreenRedControlToEachShiftCell(RedGreenSelectionUserControl redGreenSelectionUserControl1)
        {
            foreach (var shiftCell in m_ShiftsInDay)
            {
                shiftCell.SetTheGreenRedControlToEachShiftCell(redGreenSelectionUserControl1);
            }
        }

        public void cleanDay()
        {
            foreach (var shiftCell in m_ShiftsInDay)
            {
                shiftCell.CleanShiftCell();
            }
        }

    }
}
